from datetime import datetime
from enum import unique

from app import db
from tools.image import ImageDefault

def DeleteInstance(instance):
    db.session.delete(instance)
    db.session.commit()

def UpdateInstance(instance, data=None):
    def insert(obj):
        try:
            db.session.add(obj)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(f"Error: {e}")

    if instance is None:
        return

    columns = {column.name for column in instance.__table__.columns}
    if data:
        common_fields = columns & data.keys()
        for column_name in common_fields:
            if data[column_name] is not None:
                setattr(instance, column_name, data[column_name])
    insert(instance)

def PackInstance(instances):
    if not instances:
        return {}

    if not isinstance(instances, list):
        instances = [instances]

    table_name = instances[0].__class__.__name__

    instance_list = [
        {column.name: getattr(instance, column.name) for column in instance.__table__.columns}
        for instance in instances
    ]
    return {table_name: instance_list}

def QueryOneInstance(class_, filter_):
    return class_.query.filter_by(**filter_).first()

def QueryMultipleInstances(class_, filter_):
    return class_.query.filter_by(**filter_).all()

def QueryOrCreateOneInstance(class_, filter_):
    ins =  class_.query.filter_by(**filter_).first()
    if ins is None:
        ins = class_(**filter_)
    return ins

def NowStr():
    return datetime.now().strftime('%Y-%m-%d %H:%M')


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(300), nullable=False, unique=True)
    password = db.Column(db.String(300), nullable=False)

class Personal(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    intro = db.Column(db.String(120), default='该用户暂无介绍')
    uid = db.Column(db.Integer, nullable=False)
    role = db.Column(db.String(50), default='访客')
    img_icon = db.Column(db.Text, default=lambda :ImageDefault())
    img_background = db.Column(db.Text, default=lambda :ImageDefault())

class Blog(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)
    time = db.Column(db.String(30), nullable=False,default=lambda:NowStr())
    sort = db.Column(db.String(50), default='交友')
    title = db.Column(db.String(120), nullable=False,default='该帖子暂无标题')
    content = db.Column(db.Text, nullable=False,default='该帖子暂无内容')
    checked = db.Column(db.Boolean, default=False)
    passed = db.Column(db.Boolean, default=False)
    reason = db.Column(db.String(300), default='无')
    role = db.Column(db.Boolean, default=False)
    part = db.Column(db.Boolean, default=False)

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)
    pid = db.Column(db.Integer, nullable=False)
    time = db.Column(db.String(30), nullable=False,default=lambda:NowStr())
    content = db.Column(db.Text, nullable=False,default='该用户暂无评论')

class Like(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)
    pid = db.Column(db.Integer, nullable=False)
    time = db.Column(db.String(30), nullable=False,default=lambda:NowStr())

class Check(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)
    pid = db.Column(db.Integer, nullable=False,unique=True)
    time = db.Column(db.String(30), nullable=False,default=lambda:NowStr())

class Admin(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)

class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.Integer, nullable=False)
    role = db.Column(db.String(50), nullable=False,default='访客')
    part =  db.Column(db.String(50), nullable=False,default='无院系')

